module.exports = {
	_before: function () { // 通用预处理器

	},
	/**
	 * 打印订单
	 * @param {object} 待打印的订单数据
	 * @returns {bool} 返回打印状态
	 */
	
	async startPrint(order) {
		var db = uniCloud.database();
		//查询出店铺位置
		var shopData = await db.collection('shop').where({_id:order.shop_id}).get();
		var shop = shopData.data[0];
		var printerData = await db.collection('printer').where({shop_id:order.shop_id}).get();
		var printer = printerData.data;
		//推送给商家对应的打印机
		var apiUrl = 'https://api.yipaidaojia.com/index.php/api/start_printer';
		var goods = order.goods;
		var contentStr = '';
		for(var i=0;i<goods.length;i++){
			if(goods[i].sku && goods[i].goods_options){
				contentStr+=goods[i].name+'('+goods[i].sku+','+goods[i].goods_options+')'+' X'+goods[i].number+' '+' ￥'+((goods[i].price * goods[i].number)/100).toFixed(2)+'<BR><BR>';
			}else if(goods[i].sku && !goods[i].goods_options){
				contentStr+=goods[i].name+'('+goods[i].sku+')'+' X'+goods[i].number+' '+' ￥'+((goods[i].price * goods[i].number)/100).toFixed(2)+'<BR><BR>';
			}else if(!goods[i].sku && goods[i].goods_options){
				contentStr+=goods[i].name+'('+goods[i].goods_options+')'+' X'+goods[i].number+' '+' ￥'+((goods[i].price * goods[i].number)/100).toFixed(2)+'<BR><BR>';
			}else{
				contentStr+=goods[i].name+' X'+goods[i].number+' '+' ￥'+((goods[i].price * goods[i].number)/100).toFixed(2)+'<BR><BR>';
			}
			
		}
		
		var date = new Date(order.service_time.time_stamp);
		var hours = date.getHours()+8;
		if(hours < 10){
			hours = '0'+hours;
		}
		var minutes = date.getMinutes();
		if(minutes < 10){
			minutes = '0'+minutes;
		}
		if(order.type==1){
			var serviceTime = hours+'点'+minutes+'分前送达';
		}else if(order.type==2){
			var serviceTime = hours+'点'+minutes+'分前到店';
		}
		
		var createDate = new Date(order.create_date.time_stamp);
		var month = createDate.getMonth()+1;
		if(month < 10){
			month = '0'+month;
		}
		var today = createDate.getDate();
		if(today< 10){
			today = '0'+today;
		}
		var cHours = createDate.getHours()+8;
		if(cHours < 10){
			cHours = '0'+cHours;
		}
		var minutes = createDate.getMinutes();
		if(minutes < 10){
			minutes = '0'+minutes;
		}
		var createTime = createDate.getFullYear()+'-'+month+'-'+today+' '+cHours+':'+minutes;
		var tel =  order.address.phone.substr(0,3)+'****'+order.address.phone.substr(7,4);
		if(order.type==1){
			var orderType='外卖配送';
		}else if(order.type==2){
			var orderType='到店就餐';
		}
		if(order.preferential_total_price){
			var preferentialTotalPrice = order.preferential_total_price;
		}else{
			var preferentialTotalPrice = '0.00';
		}
		
		if(order.preferential){
			var preferential = order.preferential.toFixed(2)
		}else{
			var preferential ='0.00';
		}
		//拼接打印内容
		var content = "<C><L1>易派到家 #"+order.serial_number+"</L1></C><BR>"
		+"<C><B>"+shop.title+"</B></C><BR>"
		+"<L1><C>【"+orderType+"】</C></L1>"
		+"<L1><C>【"+order.service_text+"】</C></L1>"
		+"<L1><C>"+serviceTime+"</C></L1><BR>"
		+"<B>下单时间："+createTime+"</B><BR>"
		+"<B>订单号："+order.order_no+"</B><BR><BR>"
		+"<C><B>-------------------------------</B></C><BR>"
		+"<B>【隐私保护】顾客地址已隐藏，可登录商家端或骑手端查看</B><BR><BR>"
		+"<L1>姓名:"+order.address.name+"</L1><BR>"
		+"<L1>手机:"+tel+"</L1><BR>"
		+"<C><B>-------------------------------</B></C><BR>"
		+"<B>"+contentStr+"</B><BR>"
		+"<C><B>-------------------------------</B></C><BR><BR>"
		+"<L1>总价格：￥"+((order.total_fee)/100).toFixed(2)+"</L1><BR><BR>"
		+"<B>配送费:         ￥"+((order.delivery_price)/100).toFixed(2)+"</B><BR>"
		+"<B>打包费:         ￥"+((order.pack_price)/100).toFixed(2)+"</B><BR>"
		+"<B>平台优惠:       ￥"+preferentialTotalPrice+"</B><BR>"
		+"<B>满减优惠:       ￥"+preferential+"</B><BR>"
		+"<C><B>-------------------------------</B></C><BR>"
		+"<L1>备注："+order.notes+"</L1><BR><BR>"
		+"<C><B>-------------------------------</B></C><BR>"
		+"<L1>餐具："+order.tableware_number+"套餐具</L1><BR><BR>"
		+"<C><B>---------易派到家 #"+order.serial_number+"---------</B></C><BR>"
		+"<QRCODE>https://wx.yipaidaojia.com</QRCODE><BR>"
		+"<C><B>微信扫码关注易派到家公众号<B></C><BR><BR>"
		+"店铺地址："+shop.address+"<BR><BR>";
		
		
		for(var i = 0;i< printer.length;i++){
			var sn = printer[i].sn;
			var open = printer[i].is_open;
			if(open==true){
				await uniCloud.httpclient.request(apiUrl, {
				    method: 'POST',
				    data: {
				      token: '86f24d4bf67694706a347dc36f3365c0',
				      sn:sn,
					  content:content
				    },
				    contentType: 'json', // 指定以application/json发送data内的数据
				    dataType: 'json' // 指定返回值为json格式，自动进行parse
				  })
			}
			
		}
		
		
		//推送给总公司打印机
		await uniCloud.httpclient.request(apiUrl, {
		    method: 'POST',
		    data: {
		      token: '86f24d4bf67694706a347dc36f3365c0',
		      sn:'1928659187',
			  content:content
		    },
		    contentType: 'json', // 指定以application/json发送data内的数据
		    dataType: 'json' // 指定返回值为json格式，自动进行parse
		  })
		  
		  
		  
		  
		
	}
	
}
